package com.bbtShop.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbtShop.config.BizException;
import com.bbtShop.core.model.*;
import com.bbtShop.dao.ProduceRecodeDayViewMapper;
import com.bbtShop.dao.ProductRecodeMapper;
import com.bbtShop.dao.ProductRecodeViewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author manke
 * @Date 2020/10/13 16:49
 * @Version 0.0.1
 */
@Service
public class ProductRecodeService {

    @Autowired
    private ProductRecodeMapper productRecodeMapper;
    @Autowired
    private ProductRecodeViewMapper productRecodeViewMapper;
    @Autowired
    private ProduceRecodeDayViewMapper produceRecodeDayViewMapper;

    public IPage<ProductRecodeView> getLisProductRecode(Integer pageNum, Integer pageSize, ProductRecode productRecode) {
        IPage<ProductRecodeView> page = new Page<>(pageNum == null ? 1 : pageNum,
                pageSize == null ? 10 : pageSize);
        LambdaQueryWrapper<ProductRecodeView> productRecodeViewLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (productRecode.getDate() != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(productRecode.getDate());
            productRecodeViewLambdaQueryWrapper.like(ProductRecodeView::getDate, format);
        }
        IPage<ProductRecodeView> productRecodeViewIPage = productRecodeViewMapper.selectPage(page, productRecodeViewLambdaQueryWrapper);
//        IPage<Map<String, Object>> mapIPage = productRecodeViewMapper.selectMapsPage(page, productRecodeViewLambdaQueryWrapper);
//        List<ProductRecodeView> records = productRecodeViewIPage.getRecords();
//        ProductRecodeView productRecodeView = new ProductRecodeView();
//        productRecodeView.setCategoryName("合计");
//        productRecodeView.setGiveNum(100);
//        records.add(productRecodeView);
        return productRecodeViewIPage;


    }

    public void addUpdateProductRecode(ProductRecode productRecode) {
        Integer zero = 0;
        if (productRecode.getId() == null) {
            Integer residueNum =
                    (productRecode.getProductionNum() == null ? zero : productRecode.getProductionNum())
                            - (productRecode.getSalesNum() == null ? zero : productRecode.getSalesNum())
                            - (productRecode.getFailedNum() == null ? zero : productRecode.getFailedNum())
                            - (productRecode.getTestEatNum() == null ? zero : productRecode.getTestEatNum())
                            - (productRecode.getGiveNum() == null ? zero : productRecode.getGiveNum());
            if (residueNum < 0) {
                throw new BizException(ResultCode.PRODUCT_NUM_ERROR);
            }
            productRecode.setResidueNum(residueNum); //设置剩余数量
            // 判断当天是否插入
            Date date = productRecode.getDate();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(date);
            List<ProductRecode> productRecodes = productRecodeMapper.selectList(new LambdaQueryWrapper<ProductRecode>()
                    .eq(ProductRecode::getProductId, productRecode.getProductId())
                    .like(ProductRecode::getDate, format));
            if (productRecodes.size() > 0) {
                throw new BizException(ResultCode.PRODUCT_NOT_ALLOW);
            }
            productRecode.setUpdTime(new Date());
            productRecode.setAddTime(new Date());
            productRecodeMapper.insert(productRecode);
        } else {
            Integer residueNum = productRecode.getProductionNum() - productRecode.getSalesNum() - productRecode.getFailedNum() - productRecode.getTestEatNum() - productRecode.getGiveNum();
            if (residueNum < 0) {
                throw new BizException(ResultCode.PRODUCT_NUM_ERROR);
            }
            productRecode.setResidueNum(residueNum); //设置剩余数量
            productRecode.setUpdTime(new Date());
            productRecodeMapper.updateById(productRecode);
        }

    }

    public void delProductRecode(Ids ids) {
        productRecodeMapper.deleteBatchIds(ids.getIds());

    }

    public ProductRecode getProductRecodeInfo(Integer id) {
        return productRecodeMapper.selectById(id);

    }

    public IPage<ProduceRecodeDayView> getProductSalesByTime(ProduceViewTimeVo produceViewTimeVo, Integer pageNum,
                                                             Integer pageSize) {
        IPage<ProduceRecodeDayView> page = new Page<>(pageNum == null ? 1 : pageNum,
                pageSize == null ? 10 : pageSize);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String format = simpleDateFormat.format(produceViewTimeVo.getDate());
        LambdaQueryWrapper<ProduceRecodeDayView> produceRecodeDayViewLambdaQueryWrapper = new LambdaQueryWrapper<>();
        produceRecodeDayViewLambdaQueryWrapper.like(ProduceRecodeDayView::getDate, format);
        produceRecodeDayViewLambdaQueryWrapper.orderByDesc(ProduceRecodeDayView::getDate);
        return produceRecodeDayViewMapper.selectPage(page, produceRecodeDayViewLambdaQueryWrapper);


    }

    public void delProductRecodeByTime(DatesVo datesVo) {
        for (Date date : datesVo.getDates()) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(date);
            UpdateWrapper<ProductRecode> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(ProductRecode::getDate,format);
            productRecodeMapper.delete(updateWrapper);
        }
    }
}
